//
//  TUIOrderCell.m
//  TUIChat
//
//  Created by xia on 2022/6/13.
//

#import "TUIOrderCell.h"
#import <TUICore/TUIGlobalization.h>
#import <TUICore/TUIThemeManager.h>

@implementation TUIOrderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _backView = [[UIImageView alloc] init];
        [_backView setImage:TUIChatBundleThemeImage(@"chat_custom_evaluation_message_img", @"custem_chat_hongbaobeijing")];
        [self.container addSubview:_backView];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont boldSystemFontOfSize:13];
        _titleLabel.textColor = TUIChatDynamicColor(@"chat_text_message_receive_text_color", @"#000000");
        _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        [self.container addSubview:_titleLabel];
       
        _descLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 200, 180)];
        _descLabel.font = [UIFont systemFontOfSize:12];
        _descLabel.numberOfLines = 0;
//        _descLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        _descLabel.textColor = TUIChatDynamicColor(@"chat_custom_order_message_desc_color", @"#FFFFFF");
        [self.container addSubview:_descLabel];
        
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = [UIFont boldSystemFontOfSize:12];
        _priceLabel.numberOfLines = 0;
//        _priceLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        _priceLabel.textColor = TUIChatDynamicColor(@"chat_custom_order_message_price_color", @"#FF7201");
        [self.container addSubview:_priceLabel];
        _priceLabel.textAlignment = NSTextAlignmentRight;
        
        [self.bubbleView setHidden:YES];

    }
    return self;
}

- (void)fillWithData:(TUIOrderCellData *)data {
    [super fillWithData:data];
    
    self.customData = data;
    self.titleLabel.text = data.title;
    if (data.direction == MsgDirectionIncoming) {
        self.titleLabel.text = [NSString stringWithFormat:@"%@送给我了", data.link];
    }else {
        self.titleLabel.text = [NSString stringWithFormat:@"赠送礼物", data.link];
    }
    self.descLabel.text = data.desc;
    self.priceLabel.text = [NSString stringWithFormat:@"总价值:\n%@",data.price];
//    if (data.imageUrl == nil) {
//        [self.iconView setImage:TUIChatBundleThemeImage(@"chat_custom_order_message_img", @"message_custom_order")];
//    } else {
//        [self.iconView setImage:[UIImage sd_imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:data.imageUrl]]]];
//    }
    
    // tell constraints they need updating
    [self setNeedsUpdateConstraints];

    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];

    [self layoutIfNeeded];
    
}

+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}


- (void)updateConstraints {
    [super updateConstraints];
    
    
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0,0,0,0));
    }];
    
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(10);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(30);
    }];

    
    [self.priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.container.mas_bottom).offset(-5);
        make.left.mas_equalTo(10);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(45);
    }];
    
//    CGRect rect = [self.customData.desc boundingRectWithSize:CGSizeMake(200, MAXFLOAT)
//                                                    options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
//                                                 attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12] }
//                                                    context:nil];
    [self.descLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
//        make.height.mas_equalTo(rect.size.height > 180 ? 180 : rect.size.height);
    }];
    
    
}


- (void)layoutSubviews {
    [super layoutSubviews];
}

+ (CGSize)getContentSize:(TUIMessageCellData *)data {
    return CGSizeMake(220, 260);
}


@end
